<?php

define('WOWCS_UPDATE_TYPE_TOON',1);
define('WOWCS_UPDATE_TYPE_GUILD',0);


/*
 * These functions cache the constants and index that exist in wow
 */
function _wowcharsheet_get_constant($table,$key = '',$lang='') {
  global $language;

  $consts = variable_get('wowcharsheet_consts_' . $table,array());
  
  if($lang == '') {
  	$lang = $language->language;
  }

  //
  // Hack to match drupal language to XML results language
  //
  if($lang == 'en') $lang = 'en_us';

  if($key == '') {
    return $consts[$lang];
  } else {
    return isset($consts[$lang][$key])?$consts[$lang][$key]:FALSE;
  }
}

function _wowcharsheet_set_constant($lang,$table,$key,$value) {
  if($lang == 'en') $lang = 'en_us';

  $consts = variable_get('wowcharsheet_consts_' . $table,array());
  if(!array_key_exists($lang,$consts)) {
  	$consts[$lang] = array();
  }
  $consts[$lang][$key] = $value;
  variable_set('wowcharsheet_consts_' . $table,$consts);
}


function convertXmlObjToArr($obj, &$arr)
{
    $children = $obj->children();
    foreach ($children as $elementName => $node)
    {
        $nextIdx = count($arr);
        $arr[$nextIdx] = array();
        $arr[$nextIdx]['@name'] = strtolower((string)$elementName);
        $arr[$nextIdx]['@attributes'] = array();
        $attributes = $node->attributes();
        foreach ($attributes as $attributeName => $attributeValue)
        {
            $attribName = strtolower(trim((string)$attributeName));
            $attribVal = trim((string)$attributeValue);
            $arr[$nextIdx]['@attributes'][$attribName] = $attribVal;
        }
        $text = (string)$node;
        $text = trim($text);
        if (strlen($text) > 0)
        {
            $arr[$nextIdx]['@text'] = $text;
        }
        $arr[$nextIdx]['@children'] = array();
        convertXmlObjToArr($node, $arr[$nextIdx]['@children']);
    }
    return;
}  
